package src.domain;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import java.util.TimeZone;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Version;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import src.common.MessageClassification;

@Entity
public class OutgoingMessage implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long id;
	private int version;
	private String subject;
	private String content;
	private Date date;
	private TimeZone time;
	private MessageClassification messageClassification;
	private MailBox sender;
	private Set<MailBox> targetMailboxes;

	public OutgoingMessage() {
	}

	/*public OutgoingMessage(OutgoingMessageDTO outgoingMessageDTO) {
		this.id = outgoingMessageDTO.getId();
		this.version = outgoingMessageDTO.getVersion();
		this.subject = outgoingMessageDTO.getSubject();
		this.content = outgoingMessageDTO.getContent();
		this.date = outgoingMessageDTO.getDate();
		this.time = outgoingMessageDTO.getTime();
		this.messageClassification=outgoingMessageDTO.getMessageClassification();
		this.sender = new MailBox(outgoingMessageDTO.getSender());
		
		if (outgoingMessageDTO.getTargetMailboxes() != null) {
			Set<MailBox> mailBoxes = new HashSet<MailBox>();
			for (MailBoxDTO mailBoxDTO : outgoingMessageDTO.getTargetMailboxes())
				mailBoxes.add(new MailBox(mailBoxDTO));
			this.targetMailboxes = mailBoxes;
		}
		
	}*/

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Version
	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}

	@Column(length = 100)
	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	@Column(length = 1000)
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	@Column
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Column
	public TimeZone getTime() {
		return time;
	}

	public void setTime(TimeZone time) {
		this.time = time;
	}

	@Enumerated(EnumType.STRING)
	public MessageClassification getMessageClassification() {
		return messageClassification;
	}

	public void setMessageClassification(
			MessageClassification messageClassification) {
		this.messageClassification = messageClassification;
	}

	@ManyToOne
	@Fetch(FetchMode.JOIN)
	@JoinColumn(name = "idMailBox")
	public MailBox getSender() {
		return sender;
	}

	public void setSender(MailBox sender) {
		this.sender = sender;
	}

	@ManyToMany
	@Fetch(FetchMode.JOIN)
	@JoinTable(name = "Target_MailBoxes", joinColumns = @JoinColumn(name = "idOutgoingMessage"), inverseJoinColumns = @JoinColumn(name = "idMailbox"))
	public Set<MailBox> getTargetMailboxes() {
		return targetMailboxes;
	}

	public void setTargetMailboxes(Set<MailBox> targetMailboxes) {
		this.targetMailboxes = targetMailboxes;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OutgoingMessage other = (OutgoingMessage) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
